﻿///*-------------------------------------
////
////Copyright (C) 2007 开发二组 版权所有。
////
////文件名：Searcher.ascx.cs
////
////
////文件功能描述：对商品进行普通搜索
////
////
////创建标识：2007.11.22 何卫红
////修改标识：2007.12.04 何卫红
////创建描述：搜索商品..
////
////------------------------------------*/

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using JXMSTC.SmartBusiness.BFL.BLLFactory;
using JXMSTC.SmartBusiness.BFL.IBLL;

public partial class UserControl_Searcher : System.Web.UI.UserControl
{
   

    //定义商品类的WebService实例
    localhost.ProductService product = new localhost.ProductService();

    //定义保存记录的数据集
    DataSet dsProduct = new DataSet();

    
    //定义页的大小
    private int pageSize = 8;

    private static int iPageCount;  //总的页数

    private  int iRecords;    //总的记录数 

    //URL字符串参数...
    private static string categoryID = string.Empty;
    private static string productName = string.Empty;

    protected void Page_Load(object sender, EventArgs e)
    {
        //启动页面时的数据绑定
        if (!IsPostBack)
        {
            //获得URL字符串参数 ，得到 类别的ID与搜索产品的名字;
            categoryID = Convert.ToString(Request.QueryString["categoryID"]);
            productName = Convert.ToString(Request.QueryString["ProductName"]);
            
            //获取页的总数
            GetProductCount();
            
            //绑定页的选择索引
            BindDropdownListPageIndex();

            BindDataList(1);
        }
    }



    /// <summary>
    /// 绑定数据源
    /// </summary>
    /// <param name="iPageIndex">当前的页码</param>

    protected void BindDataList(int iPageIndex)
    {

        //获取页的总数
        GetProductCount();    

        //异步调用，搜索某类商品或者某类别下的某件商品
        product.SeracherProductListByConditionAndKeyCompleted += new localhost.SeracherProductListByConditionAndKeyCompletedEventHandler(product_SeracherProductListByConditionAndKeyCompleted);
        product.SeracherProductListByConditionAndKeyAsync(pageSize,iPageIndex, categoryID, productName);

        //显示当前页
        lblPage.Text = iPageIndex.ToString();

        if (iPageIndex == 1)
        {
            //如果当前页为第一页
            ddlPage.SelectedValue = "0";
        }
        else
        {
            ddlPage.SelectedValue = (iPageIndex - 1).ToString();
        }


        //显示分页按钮的可用性
        ShowButtonPaging();
        
    }

    //异步回调方法
    void product_SeracherProductListByConditionAndKeyCompleted(object sender, localhost.SeracherProductListByConditionAndKeyCompletedEventArgs e)
    {
        //定数据源
        dsProduct = e.Result;       

        //将数据视图按照一定的排序规则绑定到数据源
        DataView dv = dsProduct.Tables[0].DefaultView;
        dv.Sort = dropSort.SelectedValue + " ASC";     

        //能分页的数据源的绑定
       // ddlPage.SelectedValue = Convert.ToString(cup - 1);
        dlstSearchProduct.DataSource = dv;
        dlstSearchProduct.DataBind();
    

    }

    /// <summary>
    /// 绑定用来选择页导航的下拉列表
    /// </summary>
    protected void BindDropdownListPageIndex()
    {
        ListItem lt;
        for (int i = 0; i < iPageCount; i++)
        {
            lt = new ListItem();
            lt.Text = Convert.ToString(i + 1);
            lt.Value = i.ToString();
            ddlPage.Items.Add(lt);
        }
    }



    /// <summary>
    /// 设定分页按钮的可用性
    /// </summary>
    protected void ShowButtonPaging()
    {

        linkbtnFirst.Enabled = true;
        linkbtnPre.Enabled = true;
        linkbtnNext.Enabled = true;
        linkbtnLast.Enabled = true;

        if (iPageCount == 0 || iPageCount == 1)
        {
            linkbtnFirst.Enabled = false;
            linkbtnPre.Enabled = false;
            linkbtnNext.Enabled = false;
            linkbtnLast.Enabled = false;
        }

        if (int.Parse(lblPage.Text)<=1)
        {
            //如果是第一页，则将首页与上一页的按钮设为不可用
            linkbtnFirst.Enabled = false;
            linkbtnPre.Enabled = false;
        }
        else if (int.Parse(lblPage.Text) >= iPageCount)
        {
            //如果是最后一页，则将末页与下一页设为不可用
            linkbtnNext.Enabled = false;
            linkbtnLast.Enabled = false;
        }

        else
        {
            //其他情况全部为可用
            linkbtnFirst.Enabled = true;
            linkbtnPre.Enabled = true;
            linkbtnNext.Enabled = true;
            linkbtnLast.Enabled = true;

        }
    }

    /// <summary>
    /// 获取总的记录条数
    /// </summary>
    protected void GetProductCount()
    {
        iRecords = product.SeracherProductCountByConditionAndKey(categoryID,productName);
       
        if (iRecords % 8 == 0)
        {
            //记录数刚好可以整除页大小
            iPageCount = iRecords / 8;
        }
        else
        {
            //记录数不能整除页大小
            iPageCount = Convert.ToInt32(iRecords / 8) + 1;
        }

        lblRecords.Text = iRecords.ToString();
    }

    

    //当选择不同的排序条件时，重新进行数据绑定
    protected void dropSort_SelectedIndexChanged(object sender, EventArgs e)
    {       

        BindDataList(1);
    }

    //当选择不同的页码时发生的事件
    protected void ddlPage_SelectedIndexChanged(object sender, EventArgs e)
    {
        lblPage.Text = Convert.ToString(Convert.ToInt32(ddlPage.SelectedValue) + 1);
        int page = Convert.ToInt16(lblPage.Text);
        BindDataList(page);
    }

    //当选择第一页时的记录情况
    protected void linkbtnFirst_Click(object sender, EventArgs e)
    {
        lblPage.Text = "1";
        BindDataList(1);
    }

    //当选择最后一页时的记录情况
    protected void linkbtnNext_Click(object sender, EventArgs e)
    {
        lblPage.Text = Convert.ToString(Convert.ToInt32(lblPage.Text) + 1);
        int page = Convert.ToInt32(lblPage.Text);
        BindDataList(page);
    }

    //当选择前一页时的记录情况
    protected void linkbtnPre_Click(object sender, EventArgs e)
    {
        lblPage.Text = Convert.ToString(Convert.ToInt32(lblPage.Text) - 1);
        int page = Convert.ToInt32(lblPage.Text);
        BindDataList(page);
    }
    
    //当选择后一页时的记录情况
    protected void linkbtnLast_Click(object sender, EventArgs e)
    {
       
        BindDataList(iPageCount);
    }


    //图片的数据绑定与处理
    protected void dlstSearchProduct_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        string strProductID = ((Label)e.Item.FindControl("lblProductID")).Text;

        //在模板列中查找控件，并设置相关属性值
        ((ImageButton)e.Item.FindControl("imgBtnProducter")).Width = Unit.Parse("110px");
        ((ImageButton)e.Item.FindControl("imgBtnProducter")).Height = Unit.Parse("110px");

        ((ImageButton)e.Item.FindControl("imgBtnProducter")).ImageUrl = "../ImagInfo.aspx?productID=" + strProductID + "&&imageID=";

        ((ImageButton)e.Item.FindControl("imgBtnProducter")).PostBackUrl = "../ProductInfo.aspx?productID=" + strProductID;

    }
}
